<?php
namespace PsiFramework\Applications\AlloMediaPortal\Classes\Services;

import("Classes.Services.AbstractService");

use \PsiFramework\Applications\AlloMediaPortal\Classes\DAO\AlloMediaEntryDAO;
use \PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Classes\Model\Object;
use \PsiFramework\Classes\Services\AbstractService;
use \PsiFramework\Classes\Util\HTTP\Request;
use \PsiFramework\Classes\Util\Object\Date;
use \PsiFramework\Classes\Model\ModelInterface\IPrimitiveObject;
use \PsiFramework\Plugins\Form\Classes\DAO\FormResponseDAO;
use \PsiFramework\Plugins\Form\Classes\Util\Json\Elements\JsonFormInput;
use \PsiFramework\Plugins\Form\Classes\Util\Json\Elements\JsonFormText;
use \PsiFramework\Plugins\Form\Classes\Util\Json\Elements\JsonFormTitle;


/**
 * Bibliotheque
 * Gestion de ma bibliothèque
 *
 * @author Rémi San
 * @version beta
 */

/**
 * Service de listing des auteurs
 *
 * @package Services
 */
class ListNewResponses extends AbstractService
{

    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * The main function of the method which is called every time
     * an service is created by the front controller.
     *
     * @param $request      the HTTP request to be treated by the action
     */
    public function execute(Request $request)
    {
        parent::execute($request);

        $siteUrl = $request->getParameter("site");
        $categoryName = $request->getParameter("category");
        $since = $request->getParameter("since");

        $siteDao = DAOFactory::getDAO('AlloMediaSite');
        $categoryDao = DAOFactory::getDAO('SiteCategory');
        $entryDao = DAOFactory::getDAO('AlloMediaEntry');
        $responseIdDao = DAOFactory::getDAO('ResponseId');

        $site = $siteDao->retrieveByUri($siteUrl, false);
        $category = $categoryDao->retrieveByPartialName($categoryName, true, false);
        $entries = $entryDao->retrieveByResponses($site, $category, $since, false);

        $doc = new \DOMDocument('1.0', 'UTF-8');
        $doc->formatOutput = true;

        $allomediaXml = $doc->createElement('allomedia');

        if (!is_null($site) && !is_null($entries) && !is_null($category)) {

            $siteXml = $doc->createElement('site');
            $siteXml->setAttribute('name', $site->getUri());

            $categoryXml = $doc->createElement('categorie');
            $categoryXml->setAttribute('name', $category->getName());

            $sites = $category->getSites();
            foreach ($sites as $siteLink) {
                if ($siteLink->getSite()->getId() == $site->getId()) {
                    $categoryXml->setAttribute('id', $siteLink->getIdForSite());
                    break;
                }
            }
            
            foreach ($entries as $entry) {

                $siteMembership = null ;
                $sites = $entry->getSites();
                if (!is_null($sites)) {
                    foreach ($sites as $currentSiteMembership) {
                        $currentSite = $currentSiteMembership->getSite();
                        if ($currentSite->getId() == $site->getId()) {
                            $siteMembership = $currentSiteMembership ;
                            break;
                        }
                    }
                }

                if (!is_null($siteMembership)) {
                    $entryXml = AlloMediaEntryDAO::getAsXml($entry, $siteMembership->getSiteUserId(), $doc);
                    $categoryXml->appendChild($entryXml);
                }
            }

            $siteXml->appendChild($categoryXml);
            $allomediaXml->appendChild($siteXml);

        }

        $doc->appendChild($allomediaXml);
        
        header('Content-type: text/xml');
        echo $doc->saveXML();
    }
}
